// var i;
// var AL_GAME_HUA_i18nLabel = require("./AL_GAME_HUA_i18nLabel");
// var AL_GAME_HUA_AddPrefab = require("./AL_GAME_HUA_AddPrefab");
// var AL_GAME_HUA_Instance = require("./AL_GAME_HUA_Instance");
// var AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
// var AL_GAME_HUA_PropConfig = require("./AL_GAME_HUA_PropConfig");
// var AL_GAME_HUA_GameData = require("./AL_GAME_HUA_GameData");
// var AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
// var h = cc._decorator;
// var _ = h.ccclass;
// var P = h.property;
// var g =
//     (h.menu,
//     (function (e) {
//         function t() {
//             var t = (null !== e && e.apply(this, arguments)) || this;
//             t.type = AL_GAME_HUA_PropConfig.PropId.TIPS_GAME;
//             t.node_bg_animation = null;
//             t.icon = null;
//             t.node_number = null;
//             t.label_number = null;
//             t.node_add = null;
//             t.node_lock = null;
//             t.label_lock = null;
//             t.progress_bar_cd = null;
//             t.cd = 0;
//             t.shifou_cd_zhong = !1;
//             t.cd_dongzuo = null;
//             t.button = null;
//             t.click_callback = null;
//             t.onTouch = null;
//             return t;
//         }
//         __extends(t, e);
//         t.prototype.onLoad = function () {
//             this.button = this.getComponent(cc.Button);
//         };
//         t.prototype.chuli_youxi_zhanting = function () {
//             if (this.cd_dongzuo) {
//                 cc.director.getActionManager().pauseTargets([this.progress_bar_cd]);
//             }
//         };
//         t.prototype.chuli_youxi_jixu = function () {
//             if (this.cd_dongzuo) {
//                 cc.director.getActionManager().resumeTargets([this.progress_bar_cd]);
//             }
//         };
//         t.prototype.start = function () {
//             var e = this;
//             this.progress_bar_cd.node.active = !1;
//             this.node.on(cc.Node.EventType.TOUCH_END, this.handle_event_click, this);
//             AL_APP_SDK_MANAGER.on(
//                 AL_GAME_HUA_Define.GameEvent.CHANGE_PROP_NUM,
//                 function (t) {
//                     if (t.idx == e.type) {
//                         e.refresh();
//                     }
//                 },
//                 this
//             );
//             this.refresh();
//         };
//         t.prototype.onDestroy = function () {
//             AL_APP_SDK_MANAGER.off(this);
//         };
//         t.prototype.init = function (e) {
//             this.click_callback = e.handle_event_click;
//             this.cd = e.cd;
//         };
//         t.prototype.shezhi_anniu_shifou_youxiao = function (e) {
//             this.button.enabled = e;
//         };
//         t.prototype.xianshi_cd_jindutiao = function (e) {
//             var t = this;
//             if (void 0 === e) {
//                 e = this.cd;
//             }
//             this.shezhi_anniu_shifou_youxiao(!1);
//             this.progress_bar_cd.node.active = !0;
//             this.progress_bar_cd.progress = 1;
//             this.shifou_cd_zhong = !0;
//             if (this.cd_dongzuo) {
//                 this.cd_dongzuo.stop();
//             }
//             this.cd_dongzuo = cc
//                 .tween(this.progress_bar_cd)
//                 .to(e, {
//                     progress: 0
//                 })
//                 .call(function () {
//                     t.shezhi_anniu_shifou_youxiao(!0);
//                     t.cd_dongzuo = null;
//                     t.progress_bar_cd.node.active = !1;
//                     t.shifou_cd_zhong = !1;
//                 })
//                 .start();
//         };
//         t.prototype.refresh = function () {
//             var e = AL_GAME_HUA_GameData.GameData.getInstance();
//             var t = e.huoqu_yijiesuo_guanqia();
//             var n = AL_GAME_HUA_PropConfig.PropConfig.getInstance().huoqu_peizhi_genju_id(this.type).unlock;
//             if (t >= n) {
//                 var o = e.huoqu_prop_shuliang(this.type);
//                 this.node_number.active = o > 0;
//                 this.node_add.active = o <= 0;
//                 this.node_lock.active = !1;
//                 if (o > 0) {
//                     this.label_number.string = o.toString();
//                 }
//             } else {
//                 this.node_lock.active = !0;
//                 this.node_number.active = !1;
//                 this.node_add.active = !1;
//                 var i = this.label_lock.getComponent(AL_GAME_HUA_i18nLabel.i18nLabel);
//                 i.params = [n.toString()];
//                 i.string = "lv解锁";
//             }
//         };
//         t.prototype.huoqu_shuliang = function () {
//             return AL_GAME_HUA_GameData.GameData.getInstance().huoqu_prop_shuliang(this.type);
//         };
//         t.prototype.huoqu_shifou_jiesuo = function () {
//             return (
//                 AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() >=
//                 AL_GAME_HUA_PropConfig.PropConfig.getInstance().huoqu_peizhi_genju_id(this.type).unlock
//             );
//         };
//         t.prototype.breath_donghua = function () {
//             this.node_bg_animation.stopAllActions();
//             this.icon.stopAllActions();
//             this.node_bg_animation.scale = 1;
//             this.node_bg_animation.opacity = 255;
//             this.icon.scale = 1;
//             this.node_bg_animation.active = !0;
//             cc.tween(this.node_bg_animation)
//                 .to(0.8, {
//                     scale: 1.2,
//                     opacity: 110
//                 })
//                 .to(0.8, {
//                     scale: 1,
//                     opacity: 255
//                 })
//                 .union()
//                 .repeatForever()
//                 .start();
//             cc.tween(this.icon)
//                 .to(0.8, {
//                     scale: 1.2
//                 })
//                 .to(0.8, {
//                     scale: 1
//                 })
//                 .union()
//                 .repeatForever()
//                 .start();
//         };
//         t.prototype.tingzhi_donghua = function () {
//             this.node_bg_animation.stopAllActions();
//             this.icon.stopAllActions();
//             this.icon.scale = 1;
//             this.node_bg_animation.scale = 1;
//             this.node_bg_animation.opacity = 255;
//             this.node_bg_animation.active = !1;
//         };
//         t.prototype.handle_event_click = function () {
//             if (!AL_GAME_HUA_Instance.Instance.Game.stage.is_touch_move_state && this.button.interactable) {
//                 if (this.onTouch) {
//                     this.onTouch();
//                 }
//                 var e = AL_GAME_HUA_GameData.GameData.getInstance();
//                 if (AL_GAME_HUA_PropConfig.PropConfig.getInstance().huoqu_shuxing_shifou_yijiesuo(e.huoqu_yijiesuo_guanqia(), this.type)) {
//                     if (e.huoqu_prop_shuliang(this.type) <= 0) {
//                         AL_GAME_HUA_Instance.Instance.Game.chuli_youxi_zhanting();
//                         AL_GAME_HUA_Common.Common.xianshi_xiafang_guanggao();
//                         AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_zengjia_daoju_mianban({
//                             id: this.type,
//                             onClose: function () {
//                                 ailin_sdk_manager.yincangBannerGuanggao();
//                                 AL_GAME_HUA_Instance.Instance.Game.chuli_youxi_jixu();
//                             }
//                         });
//                     } else {
//                         {
//                             if (this.shifou_cd_zhong) {
//                                 return;
//                             }
//                             this.xianshi_cd_jindutiao();
//                             e.genggai_prop_shuliang(this.type, -1);
//                             if (this.click_callback) {
//                                 this.click_callback();
//                             }
//                         }
//                     }
//                 } else {
//                     AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_tishi_jiedian_mianban({
//                         str: "技能未解锁"
//                     });
//                 }
//             }
//         };
//         __decorate(
//             [
//                 P({
//                     type: cc.Enum(AL_GAME_HUA_PropConfig.PropId)
//                 })
//             ],
//             t.prototype,
//             "type",
//             void 0
//         );
//         __decorate([P(cc.Node)], t.prototype, "node_bg_animation", void 0);
//         __decorate([P(cc.Node)], t.prototype, "icon", void 0);
//         __decorate([P(cc.Node)], t.prototype, "node_number", void 0);
//         __decorate([P(cc.Label)], t.prototype, "label_number", void 0);
//         __decorate([P(cc.Node)], t.prototype, "node_add", void 0);
//         __decorate([P(cc.Node)], t.prototype, "node_lock", void 0);
//         __decorate([P(cc.Label)], t.prototype, "label_lock", void 0);
//         __decorate([P(cc.ProgressBar)], t.prototype, "progress_bar_cd", void 0);
//         return __decorate([_], t);
//     })(cc.Component));
// exports.default = g;


// Import necessary modules
const LabelI18n = require("./AL_GAME_HUA_i18nLabel");
const AddPrefab = require("./AL_GAME_HUA_AddPrefab");
const Instance = require("./AL_GAME_HUA_Instance");
const Define = require("./AL_GAME_HUA_Define");
const PropConfig = require("./AL_GAME_HUA_PropConfig");
const GameData = require("./AL_GAME_HUA_GameData");
const Common = require("./AL_GAME_HUA_Common");

// Cocos Creator cc decorator
const { ccclass, property, menu } = cc._decorator;

/**
 * MyComponent is a class component for managing game properties and
 * interactions such as enabling/disabling buttons, managing cooldown
 * animations, and handling game events.
 */
@ccclass
// Define the component class
class MyComponent extends cc.Component {
    @property({ type: cc.Enum(PropConfig.PropId) })
    type = PropConfig.PropId.TIPS_GAME;

    @property(cc.Node)
    node_bg_animation = null;

    @property(cc.Node)
    icon = null;

    @property(cc.Node)
    node_number = null;

    @property(cc.Label)
    label_number = null;

    @property(cc.Node)
    node_add = null;

    @property(cc.Node)
    node_lock = null;

    @property(cc.Label)
    label_lock = null;

    @property(cc.ProgressBar)
    progress_bar_cd = null;

    cd = 0;
    shifou_cd_zhong = false;
    cd_dongzuo = null;
    button = null;
    click_callback = null;
    onTouch = null;

    // Called once the component is loaded
    onLoad() {
        this.button = this.getComponent(cc.Button);
    }

    // Pause the game action
    chuli_youxi_zhanting() {
        if (this.cd_dongzuo) {
            cc.director.getActionManager().pauseTarget(this.progress_bar_cd);
        }
    }

    // Resume the game action
    chuli_youxi_jixu() {
        if (this.cd_dongzuo) {
            cc.director.getActionManager().resumeTarget(this.progress_bar_cd);
        }
    }

    // Start the component
    start() {
        this.progress_bar_cd.node.active = false;
        this.node.on(cc.Node.EventType.TOUCH_END, this.handle_event_click, this);

        AL_APP_SDK_MANAGER.on(
            Define.GameEvent.CHANGE_PROP_NUM,
            (event) => {
                if (event.idx === this.type) {
                    this.refresh();
                }
            },
            this
        );

        this.refresh();
    }

    // Cleanup when the component is destroyed
    onDestroy() {
        AL_APP_SDK_MANAGER.off(this);
    }

    // Initialize the component with specific properties
    init(params) {
        this.click_callback = params.handle_event_click;
        this.cd = params.cd;
    }

    // Set the valid state of the button
    shezhi_anniu_shifou_youxiao(isEnabled) {
        this.button.enabled = isEnabled;
    }

    // Show the cooldown bar with a specific duration
    xianshi_cd_jindutiao(duration = this.cd) {
        this.shezhi_anniu_shifou_youxiao(false);
        this.progress_bar_cd.node.active = true;
        this.progress_bar_cd.progress = 1;
        this.shifou_cd_zhong = true;

        if (this.cd_dongzuo) {
            this.cd_dongzuo.stop();
        }

        this.cd_dongzuo = cc.tween(this.progress_bar_cd)
            .to(duration, { progress: 0 })
            .call(() => {
                this.shezhi_anniu_shifou_youxiao(true);
                this.cd_dongzuo = null;
                this.progress_bar_cd.node.active = false;
                this.shifou_cd_zhong = false;
            })
            .start();
    }

    // Refresh the component to reflect the latest game data
    refresh() {
        const gameData = GameData.GameData.getInstance();
        const unlockLevel = gameData.huoqu_yijiesuo_guanqia();
        const config = PropConfig.PropConfig.getInstance().huoqu_peizhi_genju_id(this.type);
        const requiredLevel = config.unlock;

        if (unlockLevel >= requiredLevel) {
            const propCount = gameData.huoqu_prop_shuliang(this.type);

            if (propCount > 0) {
                this.node_number.active = true;
                this.node_add.active = false;
                this.label_number.string = propCount.toString();
            } else {
                this.node_number.active = false;
                this.node_add.active = true;
            }

            this.node_lock.active = false;
        } else {
            this.node_lock.active = true;
            this.node_number.active = false;
            this.node_add.active = false;

            const lockLabelComponent = this.label_lock.getComponent(LabelI18n.i18nLabel);
            lockLabelComponent.params = [requiredLevel.toString()];
            lockLabelComponent.string = "lv解锁";
        }
    }

    // Get the current count of the prop
    huoqu_shuliang() {
        return GameData.GameData.getInstance().huoqu_prop_shuliang(this.type);
    }

    // Check if the prop is unlocked
    huoqu_shifou_jiesuo() {
        const unlockLevel = GameData.GameData.getInstance().huoqu_yijiesuo_guanqia();
        const requiredLevel = PropConfig.PropConfig.getInstance().huoqu_peizhi_genju_id(this.type).unlock;
        return unlockLevel >= requiredLevel;
    }

    // Start breathing animation for the background and icon
    breath_donghua() {
        this.node_bg_animation.stopAllActions();
        this.icon.stopAllActions();
        this.node_bg_animation.scale = 1;
        this.node_bg_animation.opacity = 255;
        this.icon.scale = 1;
        this.node_bg_animation.active = true;

        cc.tween(this.node_bg_animation)
            .to(0.8, { scale: 1.2, opacity: 110 })
            .to(0.8, { scale: 1, opacity: 255 })
            .union()
            .repeatForever()
            .start();

        cc.tween(this.icon)
            .to(0.8, { scale: 1.2 })
            .to(0.8, { scale: 1 })
            .union()
            .repeatForever()
            .start();
    }

    // Stop the breathing animation
    tingzhi_donghua() {
        this.node_bg_animation.stopAllActions();
        this.icon.stopAllActions();
        this.icon.scale = 1;
        this.node_bg_animation.scale = 1;
        this.node_bg_animation.opacity = 255;
        this.node_bg_animation.active = false;
    }

    // Handler for the button click event
    handle_event_click() {
        const gameInstance = Instance.Instance.Game;

        if (!gameInstance.stage.is_touch_move_state && this.button.interactable) {
            if (this.onTouch) {
                this.onTouch();
            }

            const gameData = GameData.GameData.getInstance();
            const propConfig = PropConfig.PropConfig.getInstance();

            if (propConfig.huoqu_shuxing_shifou_yijiesuo(gameData.huoqu_yijiesuo_guanqia(), this.type)) {
                const propCount = gameData.huoqu_prop_shuliang(this.type);

                if (propCount <= 0) {
                    gameInstance.chuli_youxi_zhanting();
                    Common.Common.xianshi_xiafang_guanggao();

                    AddPrefab.AddPrefab.chuangjian_zengjia_daoju_mianban({
                        id: this.type,
                        onClose: () => {
                            ailin_sdk_manager.yincangBannerGuanggao();
                            gameInstance.chuli_youxi_jixu();
                        }
                    });
                } else {
                    if (this.shifou_cd_zhong) {
                        return;
                    }

                    this.xianshi_cd_jindutiao();
                    gameData.genggai_prop_shuliang(this.type, -1);

                    if (this.click_callback) {
                        this.click_callback();
                    }
                }
            } else {
                AddPrefab.AddPrefab.chuangjian_tishi_jiedian_mianban({
                    str: "技能未解锁"
                });
            }
        }
    }
}

exports.default = MyComponent;